package com.mobile.ado.util.message;

import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;

import com.mobile.ado.contacts.Contact;
import com.mobile.ado.util.Scheduler;

public class MessageScheduler extends Scheduler implements Runnable {

	private Message message;

	private MessageSender messageSender;
	String[] destinationAddresses;
	private Calendar targetDate;

	public MessageScheduler(Context context, ArrayList<Contact> toContacts,
			Calendar targetDate, Message message) {

		this.message = message;
		this.targetDate = targetDate;

		int size = toContacts.size();
		destinationAddresses = new String[size];
		for (int i = 0; i < size; i++) {
			destinationAddresses[i] = toContacts.get(i).getPhones().get(0)
					.getNumber();
		}

		messageSender = new MessageSender(context);

	}

	@Override
	public void run() {
		messageSender.sendMessage(message, destinationAddresses);
	}

	@Override
	public ArrayList<Long> getDelay() {
		ArrayList<Long> delays = new ArrayList<Long>();
		Calendar nowDate = Calendar.getInstance();
		nowDate.add(Calendar.MONTH, 1);
		long target = targetDate.getTime().getTime();
		long now = nowDate.getTime().getTime();
		long delay = (target - now) / 1000;
		delays.add(delay);
		return delays;
	}

	@Override
	public ArrayList<Runnable> getTaksRunnable() {

		ArrayList<Runnable> runnables = new ArrayList<Runnable>();
		runnables.add(new ScheduleWorker());
		return runnables;
	}

	@Override
	public ArrayList<Long> getPeriod() {

		return null;
	}

	private class ScheduleWorker implements Runnable {

		@Override
		public void run() {
			messageSender.sendMessage(message, destinationAddresses);
		}

	}

}
